import {
  getParameterSetting,
  //
  getCheckProjectList,
  saveOrUpdCheckProject,
  saveOrUpdParameterSetting,
} from "@/api/smart/specialmaterial/quality.js";
import { permissionsCompanyList } from "@/api/common.js";
export default {
  data() {
    return {
      companylist: [],
      companyId: "",
      formData: {
        type: '',
        itemsId: '',
        claimId: '',
        typeId: '',
        companyId: '',
        symbol: '',
      },
      list: [],
      symbol: '',
      table: {},
      showtestTypeId: null,
      showitemsId: null,
      showclaimId: null,
      showtype: null,
      loading: false,
      input: '',
      itemList: [],
      requireList: [],
      dataType: 1,
      oldtypas: [],
      // isStrictly: false
    }
  },
  watch: {
    input() {
      this.table.item = new Array();
      if (this.input == '') {
        this.table.item = this.itemList;
      } else {
        this.itemList.forEach(item => {
          if (item.name.indexOf(this.input) !== -1) {
            this.table.item.push(item);
          }
        });
      }
    },
    companyId() {
      let _this = this;
      _this.init();
    },
  },
  computed: {},
  async created() {
    this.companylist = await permissionsCompanyList()
    this.companyId = this.companylist[0].id
    this.change1(1)
  },
  methods: {
    /*混凝土原物料切换*/
    change1(num) {
      this.dataType = num
      this.table.item = this.itemList.filter((res) => res.dataType == this.dataType)
      this.table.require = this.requireList.filter((res) => res.dataType == this.dataType)
      if (this.dataType == 1) {
        this.table.types = [{ id: 1, name: '特材原料检验' }, { id: 2, name: '特材原料复检' }]
      } else {
        this.table.types = [{ id: 3, name: '特材成品入库检验' }, { id: 4, name: '特材成品复检' }]
      }
    },
    changetype() {
      let _this = this
      if (_this.showtestTypeId && _this.showitemsId && _this.showclaimId) {
        _this.formData.type = this.showtype
        _this.loading = true
      } else {
        _this.showtype = null
        _this.$message({
          message: '请先选择对应的类别',
          type: 'warning'
        });
      }
    },
    changeitemsId() {
      let _this = this;
      if (_this.loading) {
        _this
          .$confirm('请保存更改数据,否则数据将会丢失?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            cancelButtonClass: 'bottom_all_two',
            confirmButtonClass: 'bottom_all',
            type: 'warning'
          })
          .then(() => {
            _this.showitemsId = _this.formData.itemsId;
            _this.saveSet();
          })
          .catch(() => {
            _this.formData.itemsId = _this.showitemsId;
            _this.getItemlist()
            _this.loading = false;
          });
        return;
      }
      _this.formData.itemsId = _this.showitemsId;
      _this.getItemlist()
    },
    changeclaimId() {
      let _this = this;
      if (_this.loading) {
        _this
          .$confirm('请保存更改数据,否则数据将会丢失?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            cancelButtonClass: 'bottom_all_two',
            confirmButtonClass: 'bottom_all',
            type: 'warning'
          })
          .then(() => {
            _this.showclaimId = _this.formData.claimId;
            _this.saveSet();
          })
          .catch(() => {
            _this.formData.claimId = _this.showclaimId;
            _this.getItemlist()
            _this.loading = false;
          });
        return;
      }
      _this.formData.claimId = _this.showclaimId;
      _this.getItemlist()
    },
    changetestTypeId() {
      let _this = this;
      if (_this.loading) {
        _this
          .$confirm('请保存更改数据,否则数据将会丢失?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            cancelButtonClass: 'bottom_all_two',
            confirmButtonClass: 'bottom_all',
            type: 'warning'
          })
          .then(() => {
            _this.showtestTypeId = _this.formData.typeId;
            _this.saveSet();
          })
          .catch(() => {
            _this.formData.typeId = _this.showtestTypeId;
            _this.getItemlist()
            _this.loading = false;
          });
        return;
      }
      _this.formData.typeId = _this.showtestTypeId;
      _this.getItemlist()
    },
    getItemlist() {
      let _this = this;
      if (_this.showtestTypeId && _this.showitemsId && _this.showclaimId) {
        getParameterSetting({
          companyId: this.companyId,
          typeId: this.showtestTypeId,
          itemsId: this.showitemsId,
          claimId: this.showclaimId,
        }).then((res) => {
          if (res.data) {
            _this.formData.type = res.data.type
            _this.showtype = res.data.type
            // _this.isStrictly = res.data.isStrictly == 1 ? true : false
            if (_this.formData.type == 3) {
              _this.symbol = res.data.symbol
            }
            if (_this.formData.type == 1 && res.data.symbol) {
              _this.list = JSON.parse(res.data.symbol)
              this.$forceUpdate()
            }
            return
          }
          _this.showtype = null
          _this.list = []
          _this.symbol = null
          _this.formData.type = ''
        })
      }
    },
    checkName(row, index) {
      let type = false
      this.list.forEach((v, k) => {
        if (index != k && v.name == row.name) {
          type = true
        }
      })
      if (type) {
        row.name = ''
      }
    },
    addlist() {
      let _this = this
      let obj = {
        name: '',
      }
      _this.list.push(obj)
    },
    deletelist(index) {
      this.list.splice(index, 1)
    },
    /*为文本时*/
    saveSet() {
      let _this = this;
      if (_this.showtestTypeId && _this.showitemsId && _this.showclaimId) {
        if (_this.formData.type == 2) {
          _this.savetItem()
          return
        }
        if (_this.formData.type == 3) {
          _this.formData = {
            ..._this.formData,
            symbol: _this.symbol
          }
          _this.savetItem()
          return
        }
        if (_this.formData.type == 1) {
          if (this.list.length == 0 || !this.list.every(v => (v.name != ''))) {
            this.$message({
              message: '条件最少一条并且不能为空',
              type: 'warning'
            });
            return
          }
          _this.formData.companyId = _this.companyId
          _this.formData.symbol = JSON.stringify(this.list)
          _this.savetItem()
          return
        }
        _this.savetItem()
      }
    },
    savetItem() {
      this.formData.companyId = this.companyId
      saveOrUpdParameterSetting(this.formData).then((res) => {
        if (res.code == 1001) {
          this.$message({
            message: '恭喜你，保存成功',
            type: 'success'
          });
          this.loading = false;
          this.getItemlist()
        }
      })
    },

    init() {
      getCheckProjectList({ type: 1, companyId: this.companyId }).then((res) => {
        this.itemList = res.data
        this.table.item = this.itemList.filter((res) => res.dataType == this.dataType)
        this.$forceUpdate()
      })
      getCheckProjectList({ type: 2, companyId: this.companyId }).then((res) => {
        this.requireList = res.data
        this.table.require = this.requireList.filter((res) => res.dataType == this.dataType)
        this.$forceUpdate()
      })
    },
    deletetext(low) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'bottom_all_two',
        confirmButtonClass: 'bottom_all',
        type: 'warning'
      }).then(() => {
        saveOrUpdCheckProject({
          id: low.id
        }).then((res) => {
          if (res.code == 1001) {
            this.init()
          }
        })
      }).catch(() => { });
    },
    addtext(type) {
      this.$prompt('', '输入名称', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'bottom_all_two',
        confirmButtonClass: 'bottom_all',
        inputValidator: (val) => {
          if (val == '' || val == null) {
            return '名称不能为空'
          }
        }
      }).then(({
        value
      }) => {
        let data = {
          companyId: this.companyId,
          name: value,
          type: type
        }
        data.dataType = this.dataType
        saveOrUpdCheckProject(data).then(res => {
          if (res.code == 1001) {
            this.init()
          }
        })
      }).catch(() => { });
    },
  }

};